import * as types from "./mutation-types";
import Vue from "vue";

const state = {
  clientHeight: 0,
  currentIndex: "1",
  adminInfo: {},
  userMenus: [],
  leftMenu: [],
  curLeftMenuIndex: "",
  allPrompt: [], // 所有待办数量提示
  documentPrompt: {},
  userRoleKeys: "",
  openTab: [], //所有打开的路由
  activeIndex: "/main", //激活状态
  fullSearch: ""
};

const getters = {
  fullSearch: state => state.fullSearch,
  topMenus: state => state.topMenus,
  activeIndex: state => state.activeIndex,
  openTab: state => state.openTab,
  userRoleKeys: state => state.userRoleKeys,
  clientHeight: state => state.clientHeight,
  adminInfo: state => state.adminInfo,
  userMenus: state => state.userMenus,
  leftMenu: state => state.leftMenu,
  curLeftMenuIndex: state => state.curLeftMenuIndex,
  currentIndex: state => state.currentIndex,
  allPrompt: state => state.allPrompt,
  documentPrompt: state => state.documentPrompt
};

const actions = {
  setFullSearch({ commit }, value) {
    commit(types.SET_FULL_SEARCH, value);
  },
  setClientHeight({ commit }, value) {
    commit(types.SET_CLIENT_HEIGHT, value);
  },
  updateIndex({ commit }, value) {
    commit(types.UPDATE_MENU_INDEX, value);
  },
  updateUserRoleKeys({ commit }, value) {
    commit(types.UPDATE_USER_ROLE_KEYS, value);
  },
  updateUserMenus({ commit }, value) {
    commit(types.UPDATE_USER_MENUS, value);
  },
  updatePromptCount({ commit }, value) {
    commit(types.UPDATE_PROMPT_COUNT, value);
  },
  updateDocumentCount({ commit }, value) {
    commit(types.UPDATE_DOCUMENT_COUNT, value);
  },
  updateLeftMenu({ commit }, value) {
    commit(types.UPDATE_LEFT_USER_MENUS, value);
  },
  updateLeftMenuIndex({ commit }, value) {
    commit(types.UPDATE_LEFT_INDEX, value);
  },
  cleanInfo({ commit }, value) {
    window.sessionStorage.clear();
  },
  // 添加tabs
  add_tabs({ commit }, data) {
    commit(types.UPDATE_TABS, data);
  },
  // 删除tabs
  delete_tabs({ commit }, route) {
    commit(types.DELETE_TABS, route);
  },
  // 设置当前激活的tab
  set_active_index({ commit }, index) {
    commit(types.UPDATE_INDEX, index);
  },

  updateOneTopMenu({ commit }, menu) {
    commit(types.UPDATE_TOP_ONE_MENU, menu);
  }
};

const mutations = {
  [types.UPDATE_TOP_ONE_MENU](state, menu) {
    state.userMenus.map(item => {
      if (menu.id == item.id) {
        Vue.set(item, "checked", true);
      } else {
        Vue.set(item, "checked", false);
      }
    });
  },
  [types.DELETE_TABS](state, route) {
    let index = 0;
    for (let option of state.openTab) {
      if (option.fullPath === route) {
        break;
      }
      index++;
    }
    state.openTab.splice(index, 1);
  },
  [types.UPDATE_TABS](state, data) {
    let index = 0;
    for (let option of state.openTab) {
      if (option.fullPath === data.fullPath) {
        index++;
        break;
      }
    }
    if (index == 0) {
      state.openTab.push(data);
    }
  },
  [types.UPDATE_INDEX](state, index) {
    state.activeIndex = index;
  },
  [types.UPDATE_USER_ROLE_KEYS](state, payload) {
    state.userRoleKeys = payload;
  },
  [types.SET_CLIENT_HEIGHT](state, payload) {
    state.clientHeight = payload;
  },
  [types.UPDATE_MENU_INDEX](state, payload) {
    state.currentIndex = payload;
  },
  [types.SET_FULL_SEARCH](state, payload) {
    state.fullSearch = payload;
  },
  [types.UPDATE_USER_MENUS](state, payload) {
    if (payload) {
      payload.map((item, index) => {
        if (index == 0) {
          Vue.set(item, "checked", true);
        } else {
          Vue.set(item, "checked", false);
        }
      });
    }
    state.userMenus = payload;
  },
  [types.UPDATE_PROMPT_COUNT](state, payload) {
    state.allPrompt = payload;
  },
  [types.UPDATE_DOCUMENT_COUNT](state, payload) {
    state.documentPrompt = payload;
  },
  [types.UPDATE_LEFT_USER_MENUS](state, payload) {
    state.leftMenu = payload;
  },
  [types.UPDATE_LEFT_INDEX](state, payload) {
    state.curLeftMenuIndex = payload;
  },
  delFullSearch(state, moduleNull) {
    state.fullSearch = "";
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
